{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    id name  age\n",
      "0  101   张三   20\n",
      "1  102   李四   30\n",
      "2  103   王五   40\n",
      "    name  age\n",
      "101   张三   20\n",
      "102   李四   30\n",
      "103   王五   40\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({\"id\": [101, 102, 103], \"name\": [\"张三\", \"李四\", \"王五\"], \"age\": [20, 30, 40]})\n",
    "print(df)\n",
    "\n",
    "\n",
    "df = pd.DataFrame(data={\"age\": [20, 30, 40], \"name\": [\"张三\", \"李四\", \"王五\"]}, columns=[\"name\", \"age\"], index=[101, 102, 103])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['aa', 'bb', 'cc'], dtype='object')\n",
      "Index(['id', 'name', 'age'], dtype='object')\n",
      "[[101 '张三' 20]\n",
      " [102 '李四' 30]\n",
      " [103 '王五' 40]]\n",
      "2\n",
      "(3, 3)\n",
      "9\n",
      "id       int64\n",
      "name    object\n",
      "age      int64\n",
      "dtype: object\n",
      "       aa   bb   cc\n",
      "id    101  102  103\n",
      "name   张三   李四   王五\n",
      "age    20   30   40\n",
      "----------\n",
      "     id name  age\n",
      "aa  101   张三   20\n",
      "bb  102   李四   30\n",
      "     id name\n",
      "aa  101   张三\n",
      "bb  102   李四\n",
      "cc  103   王五\n",
      "     id name  age\n",
      "aa  101   张三   20\n",
      "aa    20\n",
      "bb    30\n",
      "cc    40\n",
      "Name: age, dtype: int64\n",
      "----------\n",
      "张三\n",
      "张三\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame(data={\"id\": [101, 102, 103], \"name\": [\"张三\", \"李四\", \"王五\"], \"age\": [20, 30, 40]},index=[\"aa\", \"bb\", \"cc\"])\n",
    "# index DataFrame的行索引\n",
    "print(df.index)\n",
    "# columns   DataFrame的列标签\n",
    "print(df.columns)\n",
    "# values    DataFrame的值\n",
    "print(df.values)\n",
    "# ndim  DataFrame的维度\n",
    "print(df.ndim)\n",
    "# shape DataFrame的形状\n",
    "print(df.shape)\n",
    "# size  DataFrame的元素个数\n",
    "print(df.size)\n",
    "\n",
    "# dtypes    DataFrame的元素类型\n",
    "print(df.dtypes)\n",
    "# T 行列转置\n",
    "print(df.T)\n",
    "\n",
    "\n",
    "print(\"----------\")\n",
    "# loc[] 显式索引，按行列标签索引或切片 逗号前是行切片规则，后是列切片规则\n",
    "print(df.loc[\"aa\":\"bb\"])\n",
    "print(df.loc[:,[\"id\",\"name\"]])\n",
    "# iloc[]    隐式索引，按行列位置索引或切片\n",
    "print(df.iloc[0:1])\n",
    "print(df.iloc[0:3,2])\n",
    "print(\"----------\")\n",
    "\n",
    "# at[]  使用行列标签访问单个元素\n",
    "print(df.at[\"aa\",\"name\"])\n",
    "# iat[] 使用行列位置访问单个元素\n",
    "print(df.iat[0,1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
